package com.yxj01;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class CollectionsDemo02 {
    public static void main(String[] args) {
        ArrayList<Student> array = new ArrayList<Student>();
        Student s1 = new Student("yxj", 22);
        Student s2 = new Student("mpm", 45);
        Student s3 = new Student("wkx", 10);
        Student s4 = new Student("ymm", 22);
        array.add(s1);
        array.add(s2);
        array.add(s3);
        array.add(s4);
        //使用Collections对ArrayList集合排序
        //sort(List<T> list, Comparator<? super T> c)
        Collections.sort(array, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int num = s1.getAge() - s2.getAge();
                int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
                return num2;
            }
        });
        //遍历集合
        for (Student s : array) {
            System.out.println(s.getName() + "," + s.getAge());
        }
    }
}
